<?php
namespace app\index\controller;
// 引入系统类
use think\Db;
use think\Request;
use think\Session;
class Index extends Basic
{
    public function index()
    {
        $count = Db::table('article')->join('category', 'category.id = article.cid')->field('article.*,category.id as category_id,category.type,category.cate')->where('article.status', 1)->count();
        $res = Db::table('article')->join('category', 'category.id = article.cid')->field('article.*,category.id as category_id,category.type,category.cate')->where('article.status', 1)->order('article.id', 'desc')->limit(0,5)->select();
        $this->assign('res',$res);
        $this->assign('count',$count);
        return $this->fetch();
    }
    public function get_article(){
        $start = $this->request->param('start');
        $articles = Db::table('article')->join('category', 'category.id = article.cid')->field('article.*,category.id as category_id,category.type,category.cate')->where('article.status', 1)->order('article.id', 'desc')->limit($start,2)->select();
        return $articles;
    }
    public function callback()
    {
        $o = new \SaeTOAuthV2(WB_AKEY, WB_SKEY);

        if (isset($_REQUEST['code'])) {
            $keys = array();
            $keys['code'] = $_REQUEST['code'];
            $keys['redirect_uri'] = WB_CALLBACK_URL;
            try {
                $token = $o->getAccessToken('code', $keys);
            } catch (OAuthException $e) {
            }
        }

        if ($token) {
            $_SESSION['token'] = $token;
            setcookie('weibojs_' . $o->client_id, http_build_query($token));
            $c = new \SaeTClientV2(WB_AKEY, WB_SKEY, $_SESSION['token']['access_token']);
            $ms = $c->home_timeline(); // done
            $uid_get = $c->get_uid();
            $uid = $uid_get['uid'];
            $user_message = $c->show_user_by_id($uid);//根据ID获取用户等基本信息
//            dump($user_message);die();
            $id = $user_message['id'];
            $wid = Db::table('user')->where('wid', $id)->find();
            $clientIp = $this->request->ip();
            $data = ['wid' => $user_message['id'], 'screen_name' => $user_message['screen_name'], 'name' => $user_message['name'], 'province' => $user_message['province'], 'city' => $user_message['location'], 'profile_image_url' => $user_message['profile_image_url'], 'created_at' => $user_message['created_at'], 'clientip' => $clientIp];
            if ($wid) {
                Db::table('user')->where('wid', $id)->update($data);
            } else {
                Db::table('user')->insert($data);
            }
            $userinfo = Db::table('user')->where('wid', $id)->find();
            Session::set('id', $userinfo['id']);
            Session::set('name', $user_message['screen_name']);
            Session::set('city', $user_message['location']);
            Session::set('img_url', $user_message['profile_image_url']);
            return $this->redirect('/');
        } else {
            echo "授权失败。";
        }
    }

    public function loginout()
    {
        Session::clear();
        return $this->redirect('/');
    }

    public function content()
    {
        $id = Request::instance()->param('id/d');
        $prev = Db::table('article')->where('status', 1)->where('id', '<', $id)->order('id desc')->limit(1)->value('id');
        if ($prev) {
            $prevs = Db::table('article')->where('status', 1)->where('id', $prev)->find();
            $this->assign('prevs', $prevs);
        }
        $next = Db::table('article')->where('status', 1)->where('id', '>', $id)->order('id asc')->limit(1)->value('id');
        if ($next) {
            $nexts = Db::table('article')->where('status', 1)->where('id', $next)->find();
            $this->assign('nexts', $nexts);
        }
        $res = Db::table('article')->join('category', 'category.id = article.cid')->field('article.*,category.id as category_id,category.type,category.cate')->where('article.status', 1)->where('article.id', $id)->find();
        $comment = Db::table('comment')->join('user', 'comment.user_id = user.id')->join('article', 'article.id = comment.aid')->field('comment.*,user.id as uid,user.screen_name,user.profile_image_url,user.city')->order('comment.id', 'desc')->where('comment.status', 1)->select();
        $reply = Db::table('reply')->join('user','reply.to_uid = user.id')->join('article','article.id = reply.aid')->field('reply.*,user.id as uid,user.screen_name,user.profile_image_url,user.city')->where('reply.status',1)->select();
        $this->assign('comment', $comment);
        $this->assign('reply', $reply);
        $this->assign('res', $res);
        $this->assign('id', $id);
        $this->assign('prev', $prev);
        $this->assign('next', $next);
        return $this->fetch();
    }

    public function about()
    {
        return $this->fetch();
    }

    public function article()
    {
        $suibi = Db::table('suibi')->where('status',1)->order('id', 'desc')->limit(15)->select();
        $this->assign('suibi', $suibi);
        return $this->fetch();
    }

    public function message()
    {
        return $this->fetch();
    }

    public function addcomment(Request $request)
    {
        if ($request->isAjax()) {
            $post = input('post.');
            if ($post) {
                if ($post['fid']==''){
                    $data['user_id'] = $post['user_id'];
                    $data['aid'] = $post['aid'];
                    $data['content'] = $post['content'];
                    $res = Db::table('comment')->insert($data);
                    if ($res) {
                        return 1;
                    }
                }else{
                    $data['comment_id'] = $post['cid'];
                    $data['from_uid'] = $post['fid'];
                    $data['to_uid'] = $post['user_id'];
                    $data['aid'] = $post['aid'];
                    $data['content'] = $post['content'];
                    $res=Db::table('reply')->insert($data);
                    if ($res) {
                        return 2;
                    }
                }

            }
        }
    }
}
